package com.ant.storage.service.impl;

import com.ant.storage.common.ResultVo;
import com.ant.storage.dto.SupplierPayListDto;
import com.ant.storage.mapper.SupplierPayListMapper;
import com.ant.storage.pojo.SupplierPayList;
import com.ant.storage.pojo.UserInfo;
import com.ant.storage.service.ISupplierPayListService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletContext;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author melon
 * @since 2022-03-16
 */
@Service
public class SupplierPayListServiceImpl extends ServiceImpl<SupplierPayListMapper, SupplierPayList>
        implements ISupplierPayListService {

    @Autowired
    public ServletContext servletContext;
    @Autowired
    private ISupplierPayListService payListService;
    @Override
    public ResultVo saveSupplierPayList(List<SupplierPayListDto> dto) {
        if (dto == null || dto.size() < 0) {
            return null;
        }
        UserInfo userInfo = (UserInfo) servletContext.getAttribute("userInfo");
        List<SupplierPayList> add=new ArrayList<>();
        dto.stream().map(item->{
            SupplierPayList list=new SupplierPayList();
            list.setCommodityId(item.getCommodityId());
            list.setSupplierId(item.getSupplierId());
            list.setPurchaserId(item.getPurchaserId());
            list.setAmount(item.getAmount().toString());
            list.setPrice(item.getPrice());
            list.setTotalPrice(item.getTotalPrice());
            list.setHandleId(userInfo.getNameId());
            list.setCreationTime(new Date());
            add.add(list);
            return  list;
        }).collect(Collectors.toList());
        payListService.saveBatch(add);
        return null;
    }
}
